package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC946 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Stack<Integer> st = new Stack<>();
            int i = 0, j = 0;
            while (j < popped.length) {
                if (i < pushed.length && pushed[i] == popped[j]) {
                    ++i;
                    ++j;
                } else if (!st.isEmpty() && st.peek().intValue() == popped[j]) {
                    st.pop();
                    ++j;
                } else {
                    if (i < pushed.length) st.push(pushed[i++]);
                    else return false;
                }
            }

            return st.isEmpty() && i == pushed.length && j == popped.length;
        }
    }

    public void run() {
        Solution solution = new Solution();
        //        pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
        int[] ap = InputUtil.toIntegerArray("[1,2,3,4,5]");
        int[] ai = InputUtil.toIntegerArray("[4,3,5,1,2]");
        System.out.println(solution.validateStackSequences(ap, ai));
    }

    public static void main(String[] args) throws Exception {
        LC946 an = new LC946();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
